#ifndef __GAMELAYERCONFIGFILELOADERLUA_H__
#define __GAMELAYERCONFIGFILELOADERLUA_H__

#include "GameLayerConfig.h"

namespace LoadFileLua
{
	struct SGLConfigButton
	{
		GameLayerConfigButton* Load(Ptr<LuaObject> key,Ptr<LuaObject> content)
		{
			GameLayerConfigButton* ret = new GameLayerConfigButton();
			ret->mName = key->Get<const char*>();
			ret->mWidth = content->Sub("w")->Get<int>();
			ret->mHeight = content->Sub("h")->Get<int>();
			ret->mPosX = content->Sub("x")->Get<int>();
			ret->mPosY = content->Sub("y")->Get<int>();
			ret->mZOrder = content->Sub("z")->Get<int>();
			ret->mXT = content->Sub("xt")->Get<bool>();
			ret->mYT = content->Sub("yt")->Get<bool>();

			Ptr<LuaObject> normalobj = content->Sub("img")->Sub("normal");
			if(normalobj!=0)
				ret->mNormalImage = normalobj->Get<const char*>();
			Ptr<LuaObject> selectedobj = content->Sub("img")->Sub("selected");
			if(selectedobj!=0)
				ret->mSelectedImage = selectedobj->Get<const char*>();
			Ptr<LuaObject> disableobj = content->Sub("img")->Sub("disable");
			if(disableobj!=0)
				ret->mSelectedImage = disableobj->Get<const char*>();

			Ptr<LuaObject> textobj = content->Sub("text");
			if(textobj!=0)
				ret->mText = textobj->Get<const char*>();

			Ptr<LuaObject> fontobj = content->Sub("font");
			if(fontobj!=0)
				ret->mFont = fontobj->Get<const char*>();
			Ptr<LuaObject> fontsizeobj = content->Sub("fontsize");
			if(fontsizeobj!=0)
				ret->mFontSize=fontsizeobj->Get<int>();
			
			return ret;
		}
	};

	struct SGLConfigSprite
	{
		GameLayerConfigSprite* Load(Ptr<LuaObject> key,Ptr<LuaObject> content)
		{
			GameLayerConfigSprite* ret = new GameLayerConfigSprite();

			ret->mName = key->Get<const char*>();
			ret->mWidth = content->Sub("w")->Get<int>();
			ret->mHeight = content->Sub("h")->Get<int>();
			ret->mPosX = content->Sub("x")->Get<int>();
			ret->mPosY = content->Sub("y")->Get<int>();
			ret->mZOrder = content->Sub("z")->Get<int>();
			ret->mXT = content->Sub("xt")->Get<bool>();
			ret->mYT = content->Sub("yt")->Get<bool>();

			Ptr<LuaObject> imgobj = content->Sub("img");
			if(imgobj!=0)
				ret->mImage = imgobj->Get<const char*>();

			Ptr<LuaObject> textobj = content->Sub("text");
			if(textobj!=0)
				ret->mText = textobj->Get<const char*>();
			Ptr<LuaObject> fontobj = content->Sub("font");
			if(fontobj!=0)
				ret->mFont = fontobj->Get<const char*>();
			Ptr<LuaObject> fontsizeobj = content->Sub("fontsize");
			if(fontsizeobj!=0)
				ret->mFontSize = fontsizeobj->Get<int>();

			return ret;
		}
	};


	struct SGLConfigGameLayer
	{
		GameLayerConfigLayer* Load(Ptr<LuaObject> key,Ptr<LuaObject> content)
		{
			GameLayerConfigLayer* ret = new GameLayerConfigLayer();
			ret->mName = key->Get<const char*>();
			ret->mPosX = content->Sub("x")->Get<int>();
			ret->mPosY = content->Sub("y")->Get<int>();
			ret->mXT = content->Sub("xt")->Get<bool>();
			ret->mYT = content->Sub("yt")->Get<bool>();
			ret->mWidth = content->Sub("w")->Get<int>();
			ret->mHeight = content->Sub("h")->Get<int>();
			ret->mZOrder = content->Sub("z")->Get<int>();

			Ptr<LuaObject> subitems = content->Sub("items");
			Ptr<LuaObject> subkey,subval;
			while(subitems->IterNext(subkey,subval))
			{
				GameLayerConfigElem* subelem = 0;
				CSString subtype = subval->Sub("type")->Get<const char*>();
				if(subtype=="layer")
				{
					SGLConfigGameLayer layerloader;
					subelem = (GameLayerConfigElem*)layerloader.Load(subkey,subval);
				}
				else if(subtype=="button")
				{
					SGLConfigButton buttonloader;
					subelem = (GameLayerConfigElem*)buttonloader.Load(subkey,subval);
				}
				else if(subtype=="sprite")
				{
					SGLConfigSprite layerloader;
					subelem = (GameLayerConfigElem*)layerloader.Load(subkey,subval);
				}
				if(subelem)
					ret->mSubElemLst.PushBack(subelem);
			}
			return ret;
		}
	};

	class GameLayerConfigFileLoaderLua
		:public GameLayerConfigFileLoader
	{
		virtual void LoadFile(CSString str,CSHashMap<CSString,GameLayerConfigLayer*>& map)
		{
			CLua lua;
			lua.LoadFile(str.GetBuffer());
			LuaObject pageobj(&lua,"uipages");
			Ptr<LuaObject> pagename,content;
			while(pageobj.IterNext(pagename,content))
			{
				Ptr<LuaObject> typeobj =content->Sub("type");
				CSString pagetype = typeobj->Get<const char*>();
				if(pagetype!="layer")
					continue;
				SGLConfigGameLayer layerloader;
				GameLayerConfigLayer* layer = layerloader.Load(pagename,content);
				map.Insert(layer->mName,layer);
			}
		}
	};


};

#endif
